package mercurius.fitness.client.rb.message;

import mercurius.fitness.client.Fitness;
import mercurius.fitness.client.rb.message.forms.ViewMessageForm;
import net.pleso.framework.client.bl.IAuthDataSource;
import net.pleso.framework.client.bl.forms.IViewForm;
import net.pleso.framework.client.bl.providers.IActionProvider;
import net.pleso.framework.client.bl.providers.IActionProviders;
import net.pleso.framework.client.bl.providers.IViewFormProvider;
import net.pleso.framework.client.bl.rb.IRB;
import net.pleso.framework.client.bl.rb.columns.IRBColumn;
import net.pleso.framework.client.bl.rb.columns.impl.RBColumn;
import net.pleso.framework.client.dal.IDataRow;
import net.pleso.framework.client.dal.SelectParams;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class MessageRB implements IRB, IActionProviders {
	
	private class ViewRowAction implements IViewFormProvider {

		public IViewForm getViewForm(IDataRow row) {
			if (row instanceof Message && row != null)
				return new ViewMessageForm(((Message)row).getID());
			else
				return new ViewMessageForm(null);
		}

		public String getActionCaption() {
			return null;
		}
		
	}


	private static MessageDataSet dataSet = new MessageDataSet();
	
	public static MessageDataSet getDataSet() {
		return dataSet;
	}
	
	private class RBDataSource implements IAuthDataSource {
		
		public void select(SelectParams params, AsyncCallback callback) {
			params.setLimit(Fitness.get().getDefaultRowLimit());
			dataSet.select(params, callback);
		}

		public void selectCount(SelectParams params, AsyncCallback callback) {
			params.setLimit(Fitness.get().getDefaultRowLimit());
			dataSet.selectCount(callback);
		}
	}

	public String getCaption() {
		return "Application Messages";
	}

	private static RBColumn[] rbColumns = new RBColumn[] {
	//	new RBColumn(Message.Columns.id, 30),
		new RBColumn(Message.Columns.date, 4),
		new RBColumn(Message.Columns.time, 4),
		new RBColumn(Message.Columns.level, 3),
		new RBColumn(Message.Columns.message, 30)
			
	};

	public IRBColumn[] getColumns() {
		return rbColumns;
	}

	public IAuthDataSource getDataSource() {
		return new RBDataSource();
	}

	public IActionProvider[] getActionProviders() {
		return new IActionProvider[] {
				new ViewRowAction()
		};
	}

}
